package com.ease.component.util {
   import flash.events.Event;

   import mx.controls.listClasses.AdvancedListBase;
   import mx.controls.listClasses.IDropInListItemRenderer;
   import mx.controls.listClasses.IListItemRenderer;
   import mx.events.ListEvent;

   public class AdvancedListRightClickAdapter extends ListRightClickAdapterBase {
      private static const NO_SELECTION_INDEX:int = -1;

      public function AdvancedListRightClickAdapter() {
         super(this);
      }

      override protected function isSelected(item:Object):Boolean {
         return list.selectedItems.indexOf(item) >= 0;
      }

      /**
       * @inheritDoc
       */
      override protected function setSelectedItem(item:Object):void {
         var renderer:IDropInListItemRenderer = list.itemToItemRenderer(item) as IDropInListItemRenderer;
         list.selectedIndex = renderer ? renderer.listData.rowIndex + list.verticalScrollPosition : NO_SELECTION_INDEX;

         var listItemRenderer:IListItemRenderer = list.itemToItemRenderer(item);
         if (renderer == null || renderer.listData == null || listItemRenderer == null) {
            return;
         }
         var listEvent:ListEvent = new ListEvent(ListEvent.CHANGE);
         listEvent.columnIndex = renderer.listData.columnIndex;
         listEvent.rowIndex = renderer.listData.rowIndex;
         listEvent.itemRenderer = listItemRenderer;
         list.dispatchEvent(listEvent);
      }

      [Bindable("change")]
      [Inspectable(category = "General")]
      public function get list():AdvancedListBase {
         return AdvancedListBase(target);
      }

      public function set list(value:AdvancedListBase):void {
         if (target != value) {
            target = value;
            dispatchEvent(new Event(Event.CHANGE));
         }
      }
   }
}
